<template>
  <Dialog v-model="dialogVisible" width="500px" title="添加数据库" maxHeight="300px" @close="closeHandle">
    <el-form ref="ruleFormRef" :model="formData" :rules="formRules" label-width="80px" class="w-[400px]">
      <el-form-item label="数据库名" prop="schemaDBName" required>
        <el-input v-model="formData.schemaDBName" placeholder="请输入" clearable maxlength="120" />
      </el-form-item>
      <el-form-item label="字符集" prop="charSet" required>
        <el-select v-model="formData.charSet" class="select-c w-[100%]" clearable placeholder="请选择">
          <el-option v-for="item in charSetSelectOption" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="schemaUser" required>
        <el-input v-model="formData.schemaUser" placeholder="请输入" clearable maxlength="120" />
      </el-form-item>
      <el-form-item label="密码" prop="schemaKey" required>
        <el-input v-model="formData.schemaKey" placeholder="请输入" maxlength="120">
          <template #suffix>
            <Icon icon="ant-design:sync-outlined" class="cursor-pointer" :size="18" @click="getUuidHandle"></Icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input
v-model="formData.note" type="textarea" placeholder="请输入" clearable
          :autosize="{ minRows: 2, maxRows: 6 }" show-word-limit maxlength="220" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeHandle">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="saveHandle(ruleFormRef)">提交</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch, defineEmits, reactive, onMounted } from 'vue'
import { setCreateSchemaAPI } from "@/api/systemMonitor"
import { propTypes } from '@/utils/propTypes'
import { randomPassword } from '@/utils/index'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['update:modelValue', 'confirm'])

const props = defineProps({
  modelValue: propTypes.bool.def(false),
})
//默认表单
const defaultFormData = {
  schemaDBName: "",
  schemaUser: "",
  schemaKey: "",
  charSet: "utf8mb4",
  note: "",
}

const loading = ref(false)

const formData = reactive({
  ...defaultFormData
})

const ruleFormRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  schemaDBName: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  schemaUser: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  schemaKey: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  charSet: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
})
onMounted(() => {
  getUuidHandle();
})


//弹出框状态
const dialogVisible = ref(props.modelValue)
// charSet 选项
let charSetSelectOption = ref([{
  label: 'utf-8',
  value: 'utf-8',
}, {
  label: 'utf8mb4',
  value: 'utf8mb4',
}, {
  label: 'gbk',
  value: 'gbk',
}, {
  label: 'big5',
  value: 'big5',
}])
watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal;
})
//提交
const saveHandle = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      loading.value = true;
      setCreateSchemaAPI({
        ...formData,
      }).then(() => {
        ElMessage.success("添加成功")
        closeHandle()
        emit('confirm')
      }).finally(() => {
        loading.value = false;
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
//关闭
const closeHandle = () => {
  dialogVisible.value = false;
  //初始化数据
  Object.assign(formData, defaultFormData);
  getUuidHandle()
  emit('update:modelValue', dialogVisible)
}
//获取随机的密码
const getUuidHandle = () => {
  formData.schemaKey = randomPassword(16)
}

</script>

<style lang="less" scoped>

</style>
